import 'package:brightness/brightness.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  double value = 50;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('获取亮度'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            StreamBuilder(
                stream: GetBrightness.stream,
                builder: (context, snapshot) {
                  return Text("当前亮度：${snapshot.data}");
                }),
            const Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                "注意：android是标准Lux亮度，IOS则需要通过调节阀值获取真实Lux亮度，请勿遮挡摄像头",
                style:
                    TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () {
                    GetBrightness.startBrightnessStream();
                  },
                  child: const Text("开始监听亮度"),
                ),
                const SizedBox(
                  width: 20,
                ),
                ElevatedButton(
                  onPressed: () {
                    GetBrightness.stopBrightnessStream();
                  },
                  child: const Text("结束监听亮度"),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Row(
                children: [
                  Text('亮度阀值(IOS有效)：${value.toStringAsFixed(2)}'),
                  Expanded(
                    child: Slider(
                        max: 500,
                        min: 1,
                        value: value,
                        onChanged: (v) {
                          setState(() {
                            value = v;
                            GetBrightness.setSliderValue(value);
                          });
                        }),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
